package com.magicsoft.share.rx_retrofit_network.location.rxandroid;

import android.content.Context;

import com.magicsoft.share.R;

import cn.pedant.SweetAlert.SweetAlertDialog;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by lyf
 */
public class LoadingTransformer<T> implements ObservableTransformer<T, T> {
    private final Context mContext;
    private SweetAlertDialog dialog;

    public LoadingTransformer(@NonNull Context context) {
        mContext = context;
        dialog  = new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE);
        dialog.getProgressHelper().setBarColor(R.color.main);
        dialog.setTitleText("加载中...");
        dialog.setCancelable(true);
    }



    @Override
    public ObservableSource<T> apply(Observable<T> upstream) {

        return upstream
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        dialog.show();
                    }
                }).doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                       dialog.dismiss();
                    }
                }).doOnNext(new Consumer<T>() {
                    @Override
                    public void accept(T t) throws Exception {
                        if (t == null) {

                            dialog.dismiss();
                        }
                        dialog.dismiss();
                    }
                });
    }

//    @Override
//    public ObservableSource<T> apply(Observable<T> upstream) {
//        return upstream
//                .observeOn(AndroidSchedulers.mainThread())
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
//                        mLoading.onLoading();
//                    }
//                }).doOnError(new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        mLoading.onError();
//                    }
//                }).doOnNext(new Consumer<T>() {
//                    @Override
//                    public void accept(T t) throws Exception {
//                        if (t == null) {
//                            mLoading.onEmpty();
//                        }
//                        mLoading.onSuccess();
//                    }
//                });
//    }

    public interface LoadingInterface {
        void onLoading();

        void onSuccess();

        void onError();

        void onEmpty();
    }
}
